//
//  ChatMsgViewModel.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/2/7.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit
import HandyJSON
import SwifterSwift

enum ChatMsgUserType: Int {
    case patient
    case doctor
    

    
    var msgBgImg: UIImage? {
        switch self {
        case .patient: return UIImage(named: "msg_sender_bg_normal")
        case .doctor: return UIImage(named: "msg_receiver_bg_normal")
        }
    }
    
    var textColor: UIColor {
        switch self {
        case .patient: return .white
        case .doctor: return .textGray
        }
    }
    
    var recordLogo: String {
        switch self {
        case .doctor: return "chat_record_list_icon_color"
        case .patient: return "chat_record_list_icon"
        }
    }
    
    func secondsTextColor(isPlaying: Bool) -> UIColor {
        switch self {
        case .patient: return isPlaying ? .warning : .white
        case .doctor: return isPlaying ? .warning : .textGray
        }
    }
}

enum ChatMsgType: Int {
    case text
    case image
    case voice
    case video
    case music
    case news
    case prescription
    case unstart
    case start
    case end
}

struct ChatMsgModel: HandyJSON {
    /// 发送时间
    var sendTime: String?
    /// 0 - 患者  1 - 医生
    var fromType: Int = 0
    /// 消息id
    var messageId: String?
    /// 消息内容
    var content: String?
    /// 消息类型
    var messageType: Int = 0
    /// 高清图
    var src: String?
    /// 语音消息时常
    var time: Int = 0
    
    var isPlaying: Bool = false
    
    /// 处方
    var prescription: PrescriptionDetailModel?
    
    // MARK: - 自定义
    var chatType: Int = 2
    var cmd: Int = 0
    var createTime: TimeInterval = 0
    var diagnoseId: String?
    var docId: String?
    var docName: String?
    var from: Int?
    var msgType: Int = 0
    var roleType: Int = 0
    var testType: Int = 0
    var to: String?
    var clientId: String?
    var picType: String?
    
    var isSendSuccess: Bool = true
    var isSending: Bool = false
}

class ChatMsgViewModel: NSObject {
    let msgModel: ChatMsgModel
    
    init(model: ChatMsgModel) {
        self.msgModel = model
    }
    
    var sendTime: String { return msgModel.sendTime ?? "" }
    
    var msgContent: String { return msgModel.content ?? "" }
    
    /// 高清图
    var src: String { return msgModel.src ?? "" }
    
    /// 语音消息时常
    var time: Int { return msgModel.time }
    
    var isPlaying: Bool { return msgModel.isPlaying }
    
    var audioLogoColor: UIColor {
        switch userType {
        case .doctor: return isPlaying ?UIColor.mainColor :UIColor.mainColor
        case .patient: return isPlaying ? UIColor.white : UIColor.white
        }
    }
    
    var timeLabelColor: UIColor {
        switch userType {
        case .doctor: return isPlaying ? UIColor.warning :UIColor.mainColor
        case .patient: return isPlaying ? UIColor.warning : UIColor.white
        }
    }
    
    var userType: ChatMsgUserType {
        return ChatMsgUserType(rawValue: msgModel.fromType) ?? .patient
    }
    
    var messageId: String { return msgModel.messageId ?? "" }
    
    var messageType: ChatMsgType {
        return ChatMsgType(rawValue: msgModel.messageType) ?? .text
    }
    
    var isSendSuccess: Bool { return msgModel.isSendSuccess }
    var isSending: Bool { return msgModel.isSending }
    
    /// 气泡
    var bubbleImage: UIImage? {
        return userType.msgBgImg?.resizableImage(
            withCapInsets: UIEdgeInsets(top: 30, left: 28, bottom: 85, right: 28),
            resizingMode: .stretch)
    }
    
    var cellHeight: CGFloat {
        switch messageType {
        case .unstart: return 80
        case .text:
            let calc_max = UIScreen.main.bounds.size.width - (kChatMargin + kChatAvatarWidth + kChatMargin + 17 + kChatMargin * 2)
            let width = calc_max > 240 ? 240 : calc_max
            return msgContent.height(font: .regular(16), width: width) + 56 + 10
        case .voice: return 88
        case .image: return 185
        case .prescription: return 293
        case .start, .end: return 110
        default: return 0
        }
    }
    
    var prescriptionDetailVm: PrescriptionDetailViewModel {
        return PrescriptionDetailViewModel(model: msgModel.prescription ?? PrescriptionDetailModel())
    }
}
